package pers.lmd.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import pers.lmd.convert.TeacherConvert;
import pers.lmd.domain.R;
import pers.lmd.domain.dto.TeacherDTO;
import pers.lmd.domain.entity.Teacher;
import pers.lmd.domain.entity.User;
import pers.lmd.common.constant.ResultEnum;
import pers.lmd.mapper.UserMapper;
import pers.lmd.service.ITeacherService;
import pers.lmd.mapper.TeacherMapper;
import org.springframework.stereotype.Service;
import pers.lmd.common.utils.ResultUtil;

/**
* @author 10149
* @description 针对表【teacher】的数据库操作Service实现
* @createDate 2023-04-11 22:11:40
*/
@RequiredArgsConstructor
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements ITeacherService {

    private final UserMapper userMapper;

    @Override
    public R<Void> addTeacher(TeacherDTO teacherDTO){
        Teacher teacher = TeacherConvert.INSTANCES.toTeacher(teacherDTO);
        baseMapper.insert(teacher);
        // 获取老师id
        Long id = teacher.getId();
        // 创建用户对象
        User user = new User();
        // 设置id
        user.setId(id);
        // 密码初始化并加密
        user.setPassword(BCrypt.hashpw("123456"));
        user.setRole(1);
        userMapper.insert(user);
        return ResultUtil.success(ResultEnum.SUCCESS);
    }

    @Override
    public String getAvatar(){
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        Long id = StpUtil.getLoginIdAsLong();
        wrapper.select(Teacher::getAvatar).eq(Teacher::getId,id);
        return getOne(wrapper).getAvatar();
    }

}




